package com.wildlife.springboot.controller;

import com.wildlife.springboot.service.*;
import com.wildlife.springboot.service.IAllCategoriesService;
import com.wildlife.springboot.service.IIllegalProductService;
import com.wildlife.springboot.service.IIllegalService;
import com.wildlife.springboot.service.IMainAccidentService;
import com.wildlife.springboot.service.IRareEndangeredService;
import com.wildlife.springboot.service.IThreeZonesService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/dashboard")
public class DashboardController {

    @Autowired
    private IAllCategoriesService allCategoriesService;

    @Autowired
    private IRareEndangeredService rareEndangeredService;

    @Autowired
    private IThreeZonesService threeZonesService;

    @Autowired
    private IMainAccidentService mainAccidentService;

    @Autowired
    private IIllegalService illegalService;

    // 类成员变量用于存储统计结果
    private long allCategoriesCount;
    private long rareEndangeredCount;
    private long threeZonesCount;
    private long mainAccidentCount;
    private long illegalProductCount;

    // 在构造方法中初始化统计数据
    @PostConstruct
    public void init() {
        this.allCategoriesCount = allCategoriesService.count();
        this.rareEndangeredCount = rareEndangeredService.count();
        this.threeZonesCount = threeZonesService.count();
        this.mainAccidentCount = mainAccidentService.count();
        this.illegalProductCount = illegalService.count();
    }

    // 获取所有的总和
    @GetMapping("/total-count")
    public ResponseEntity<Integer> getTotalCount() {

//        long allCategoriesCount = allCategoriesService.count();
//        // 获取四个表的行数
//        long rareEndangeredCount = rareEndangeredService.count();
//        long threeZonesCount = threeZonesService.count();
//        long mainAccidentCount = mainAccidentService.count();
//        long illegalProductCount = illegalProductService.count();

        // 计算四个表的总和，并返回
        long totalCount = allCategoriesService.count() + rareEndangeredService.count() + threeZonesService.count() + mainAccidentService.count() + illegalService.count();
        // 将总和转换为 Integer 类型并返回
        return ResponseEntity.ok((int) totalCount);
    }

    // 获取珍稀濒危、三区四带、非法制品三张表的总和
    @GetMapping("/all-count")
    public ResponseEntity<Integer> getAllCount() {
        // 计算四个表的总和，并返回
        long totalCount = rareEndangeredService.count() + threeZonesService.count() + mainAccidentService.count() + illegalService.count();
        // 将总和转换为 Integer 类型并返回
        return ResponseEntity.ok((int) totalCount);
    }

    // 获取额外3000的总和
    @GetMapping("/other-count")
    public ResponseEntity<Integer> getOtherCount() {
        // 计算四个表的总和，并返回
        long totalCount = allCategoriesService.count();
        // 将总和转换为 Integer 类型并返回
        return ResponseEntity.ok((int) totalCount);
    }

    @GetMapping("/rare-count")
    public ResponseEntity<Integer> getRareCount() {

        long totalCount = rareEndangeredService.count();;

        // 将总和转换为 Integer 类型并返回
        return ResponseEntity.ok((int) totalCount);
    }

    @GetMapping("/three-count")
    public ResponseEntity<Integer> getThreeCount() {

        long totalCount = threeZonesService.count();;

        // 将总和转换为 Integer 类型并返回
        return ResponseEntity.ok((int) totalCount);
    }

    @GetMapping("/main-count")
    public ResponseEntity<Integer> getMainCount() {

        long totalCount = mainAccidentService.count();;

        // 将总和转换为 Integer 类型并返回
        return ResponseEntity.ok((int) totalCount);
    }

    @GetMapping("/illegal-count")
    public ResponseEntity<Integer> getIllegalCount() {

        long totalCount = illegalService.count();;

        // 将总和转换为 Integer 类型并返回
        return ResponseEntity.ok((int) totalCount);
    }
}
